<?php
header('Content-type: application/json;charset=utf-8');
require_once(dirname(__FILE__) . '/../include/config.inc.php');
require_once "function.php";
//获取 post的json数据
//获取 goodsid
$uid = $_SESSION['id'];
$userInfo = "";
$id = htmlspecialchars($_POST['id']);
$op = htmlspecialchars($_POST['op']);
if (isset($id)) {
    $time = time();
    //获取工单的时间
    $k = $dosql->GetOne("select post_time,jindu,manageid from `#@__order` where id='$id'");
    $post_time = $k['post_time'] + 1800;
    $jindu = $k['jindu'];
    $old_manageid = $k['manageid'];

    //催单
    if ($op == 'cui') {
        //判断是否超时
        if ($time > $post_time) {
            $json_data = array('msg' => 'ok');
            $dosql->ExecNoneQuery("insert into `#@__order_log`(orderid,op,op_time,jindu) values ('$id','用户催单','$time','$jindu')");
        } else {
            $json_data = array('msg' => 'error');


        }

        $errno = array('errno' => 0);

    }
    //撤单
    if ($op == 'chedan') {
        //判断是否超时
        if ($jindu < 4) {
            $json_data = array('msg' => 'ok');
            //修改订单状态
            $dosql->ExecNoneQuery("update  `#@__order` set jindu='0' where id='$id' ");
            //记录工单操作
            $dosql->ExecNoneQuery("insert into `#@__order_log`(orderid,op,op_time,jindu,op_manageid) values ('$id','发布人撤单','$time','$jindu','$uid')");
        } else {
            $json_data = array('msg' => 'error');
        }

    }

    //接单
    if ($op == 'jiedan') {
        //判断是否超时
        if ($jindu == '3') {
            $manageid = $_POST['manageid'];
            $json_data = array('msg' => 'ok');
            //修改订单状态
            $dosql->ExecNoneQuery("update  `#@__order` set jindu='4',qiandao='' where id='$id' ");
            //记录工单操作
            $dosql->ExecNoneQuery("insert into `#@__order_log`(orderid,op,op_time,jindu,manageid,jiedan,latitude,longitude,accuracy,address,address_name,op_manageid) values ('$id','工程师接单','$time','4','$old_manageid','true','$latitude','$longitude','$accuracy','$address','$address_name','$uid')");
        } else {
            $json_data = array('msg' => 'error');
        }
    }

    //抢单
    if ($op == 'qiangdan') {
        //判断是否超时
        if ($jindu == '1' || $jindu=='2' || $jindu=='3') {
            $manageid = $_POST['manageid'];
            $json_data = array('msg' => 'ok');
            //修改订单状态
            $dosql->ExecNoneQuery("update  `#@__order` set jindu='4',manageid='$manageid' where id='$id' ");
            //记录工单操作
            $dosql->ExecNoneQuery("insert into `#@__order_log`(orderid,op,op_time,jindu,manageid,jiedan,latitude,longitude,accuracy,address,address_name,op_manageid) values ('$id','工程师抢单成功','$time','4','$old_manageid','true','$latitude','$longitude','$accuracy','$address','$address_name','$uid')");
        } else {
            $json_data = array('msg' => 'error');
        }
    }

    //签到
    if ($op == 'qiandao') {
        //判断是否超时
        if ($jindu == '4') {
            $manageid = $_POST['manageid'];
            $remark = $_POST['content'];
            $picfiles = htmlspecialchars($_POST['picfiles']);

            $picfiles_str = "";
            if ($picfiles != "") {
                $picfiles_arr = explode(",", $picfiles);
                $picfiles_str = json_encode($picfiles_arr);

            }

            $json_data = array('msg' => 'ok');
            //修改订单状态
            $dosql->ExecNoneQuery("update  `#@__order` set qiandao='true' where id='$id' ");
            //记录工单操作
            $dosql->ExecNoneQuery("insert into `#@__order_log`(orderid,op,op_time,jindu,manageid,qiandao,latitude,longitude,accuracy,address,address_name,op_manageid,remark,picurls) values ('$id','工程师签到','$time','4','$old_manageid','true','$latitude','$longitude','$accuracy','$address','$address_name','$uid','$remark','$picfiles_str')");
        } else {
            $json_data = array('msg' => 'error');
        }
    }


    $errno = array('errno' => 0);

    $json_json = array_merge($errno, $json_data);
    $data = json_encode($json_json);
    echo $data;


}


?>